
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.view.*;
import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;


/**
 * 内容api
 */
 
@Api(tags = "ProjectContent",description = "内容")
@RestController
public class ProjectContentController {
    
    @Autowired
    private ProjectContentDao dao;
   
    /**
     * 获取内容集合
     * @return api结果
     */
    @ApiOperation(value = "获取内容集合")
    @RequestMapping(value =  "/api/ProjectContent/",method = {RequestMethod.GET})
    public ApiResult<List<ProjectContent>> Get(){
         ApiResult<List<ProjectContent>> result=new ApiResult<List<ProjectContent>>();
        try 
        {
            List<ProjectContent> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取内容信息
     * @param id 内容ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取内容信息")
    @RequestMapping(value =  "/api/ProjectContent/{id}",method = {RequestMethod.GET})
    public ApiResult<ProjectContent> Get(@PathVariable Long id){
        ApiResult<ProjectContent> result=new ApiResult<ProjectContent>();
        try
        {
            ProjectContent model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改内容信息
     * @param id 内容ID
     * @param model 内容信息
     * @return api结果
     */
    @ApiOperation(value = "修改内容信息")
    @RequestMapping(value =  "/api/ProjectContent/{id}",method = {RequestMethod.PUT})
    public ApiResult<ProjectContent> Edit(@PathVariable Long id,@RequestBody ProjectContent model){
        ApiResult<ProjectContent> result=new ApiResult<ProjectContent>();
        try
        {
            model.setProjectContentId(id);
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加内容信息
     * @param model 内容信息
     * @return api结果
     */
    @ApiOperation(value = "添加内容信息")
    @RequestMapping(value =  "/api/ProjectContent/",method = {RequestMethod.POST})
    public ApiResult<ProjectContent> Add(@RequestBody ProjectContent model){
         ApiResult<ProjectContent> result=new ApiResult<ProjectContent>();
        try
        {
            model.setCreateDate(new Date());
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 查询内容
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询内容")
    @RequestMapping(value =  "/api/PageContentType/Search",method = {RequestMethod.POST})
    public ApiResult<List<ProjectContent>> Search(@RequestBody PageContentType model){
         ApiResult<List<ProjectContent>> result=new ApiResult<List<ProjectContent>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"Project_Content_Id") ;            
            Page<ProjectContent> page = dao.findByWhere(model.getName(),model.getProjectContentTypeId(),model.getProjectId(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    /**
     * 删除内容信息
     * @param id 内容ID
     * @return api结果
     */
    @ApiOperation(value = "删除内容信息")
    @RequestMapping(value =  "/api/ProjectContent/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
